﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BacteriaSimulation.Simulation
{
    class Cyanobacterium : Bacterium
    {
        public Cyanobacterium(SimulationPoint position, Vector2D speed, Double energy, Double maxEnergy,
            Double energyIncrement, Bacterium brother) : base (
            position, speed, energy, maxEnergy, energyIncrement, brother)
        {
        }

        public Cyanobacterium(SimulationPoint position, Vector2D speed, Double energy, Double maxEnergy,
            Double energyIncrement)
            : base(position, speed, energy, maxEnergy, energyIncrement, null)
        {
        }
            
        public Cyanobacterium(SimulationPoint position, Vector2D speed, Double energy)
            : this(position, speed, energy, SimulationEnvironment.MAX_ENERGY, SimulationEnvironment.ENERGY_INCREMENT, null)
        {
        }

        public override void PerformSimulationStep(SimulationEnvironment env)
        {
            // the bacterium cannot grow more than his MaxEnergy parameter
            Double maxEnergyIncrement = Math.Min(EnergyIncrement, MaxEnergy - Energy);
            // the bcteriuum cannot grow more than the left avalaible energy resource in the environment
            maxEnergyIncrement = Math.Min(maxEnergyIncrement, env.AvalaibleEnergyResource);

            // This "if" is just to be sure that we never exceed the maximum energy level for more than one simulation cycle
            if (Energy > MaxEnergy)
            {
                Energy = MaxEnergy;
            }
            else if (maxEnergyIncrement > 0)
            {
                Grow(maxEnergyIncrement, env);
            }

            if (NewSpeed != null)
            {
                Speed = NewSpeed;
                NewSpeed = null;
            }
        }

        public override void WallCollisionDetected(WALL_COLLISION_TYPE collisionType)
        {
            switch (collisionType)
            {
                case WALL_COLLISION_TYPE.BOTTOM:
                    {
                        this.Speed.YComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.RIGHT:
                    {
                        this.Speed.XComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.TOP:
                    {
                        this.Speed.YComponent *= -1;
                        break;
                    }
                case WALL_COLLISION_TYPE.LEFT:
                    {
                        this.Speed.XComponent *= -1;
                        break;
                    }
            }
        }

        public override void BacteriumCollisionDetected(Bacterium otherBacterium, SimulationEnvironment env)
        {
            //if (collidedBacterium == Brother)
            //{
            //    return;
            //}

            //NewSpeed = collidedBacterium.Speed.Clone();
            //Speed.XComponent *= -1;
            //Speed.YComponent *= -1;
        }
    }
}
